package com.zhou.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;

public class EncodeUtil {

    private EncodeUtil() {

    }

    private static final Logger logger = LoggerFactory.getLogger(EncodeUtil.class);

    /**
     * MD5加密32位
     *
     * @param encryptStr 要加密数据
     * @return 32位加密结果
     */
    public static String md5Encrypt32(String encryptStr) {
        logger.info("******** 字符串: {} 加密开始", encryptStr);
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] md5Bytes = md5.digest(encryptStr.getBytes());
            StringBuilder hexValue = new StringBuilder();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = (md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            // 加密结果转换为大写
            return hexValue.toString().toUpperCase().replace("-", "");
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
